﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PlusTween
{
    public class PlusTweenBase : MonoBehaviour
    {
        public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0, 0, 1, 1), new Keyframe(1, 1, -2, 0));
        public float duration = 0.4f;
        public float delayIn = 0;
        public float delayOut = 0;
        public bool isBronOrigin = false;

        protected UITweener _tween;
     
        public System.Action onFinishIn { get; set; }
        public System.Action onFinishOut { get; set; }

        private bool m_isActive = false;
        private bool isStartIn = false;
        private bool isStartOut = false;
        private bool isFinishIn = false;
        private bool isFinishOut = false;

        private float time = 0;

        private void Update()
        {
            if (m_isActive)
            {
                if (isStartOut)
                {
                    time += Time.deltaTime;
                    if (time >= delayOut) { StartOut(); isStartOut = false; }
                }
                else if (isStartIn)
                {
                    time += Time.deltaTime;
                    if (time >= delayIn) { StartIn(); isStartIn = false; }
                }
                else if (isFinishOut)
                {
                    time += Time.deltaTime;
                    if (time > delayOut + duration) { FinishOut(); isFinishOut = false; }
                }
                else if (isFinishIn)
                {
                    time += Time.deltaTime;
                    if (time > delayIn + duration) { FinishIn(); isFinishIn = false; }
                }
                else
                {
                    time = 0;
                    m_isActive = false;
                }
            }
        }

        public virtual void SetActive(bool isActive, bool isTween )
        {
            PlusTweenTools.SetActive_NoPlusTween( this.gameObject, true );
            if (isTween)
            {
                if (isActive)
                {
                    isStartIn = true;
                    isFinishIn = true;

                    isStartOut = false;
                    isFinishOut = false;

                    SetValueFrom();
                }
                else
                {
                    isStartIn = false;
                    isFinishIn = false;


                    isStartOut = true;
                    isFinishOut = true;

                    SetValueTo();

                }
                time = 0;
                m_isActive = true;
            }
            else
            {
                // 没有动画
                if (isActive) { SetValueTo(); FinishIn(); }
                else { SetValueFrom(); FinishOut(); }
            }
        }

        public virtual void StartIn()
        { }

        public virtual void StartOut()
        { }

        public virtual void FinishIn()
        {
            if (null != onFinishIn) { onFinishIn(); }
        }

        public virtual void FinishOut()
        {
            PlusTweenTools.SetActive_NoPlusTween(this.gameObject, false);
            if (null != onFinishOut) { onFinishOut(); }
        }

        public virtual void SetValueFrom() { }
        public virtual void SetValueTo() { }

    }
}